package com.edu.controller;

import com.edu.bean.User;
import com.edu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @Author:何包蛋
 * @Date :2022/11/15 - 11 - 15 - 20:28
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login.do")
    public ModelAndView login(String username , String password){
        ModelAndView modelAndView = new ModelAndView();
        int i = userService.loginUser(username,password);
        if (i==1){
            modelAndView.setViewName("book");
            modelAndView.addObject("msg",username);
        }else if(i==0){
            modelAndView.setViewName("login.jsp");
            modelAndView.addObject("msg","用户名不存在！！！");
        }else if(i==-1){
            modelAndView.setViewName("login.jsp");
            modelAndView.addObject("msg","密码错误！！！");
        }
        return modelAndView;
    }
    @PostMapping("/regiest.do")
    public ModelAndView regiest(User u){
        ModelAndView modelAndView = new ModelAndView();
        int i = userService.registUser(u);
        modelAndView.setViewName("regiest.jsp");
        if (i==1){
            modelAndView.addObject("msg","注册成功！！！");
        } else if (i==0) {
            modelAndView.addObject("msg","注册失败！！!");
        } else if (i==-1) {
            modelAndView.addObject("msg","用户名已存在！！！");
        }
        return modelAndView;
    }

}
